﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UIHelper {

    /// <summary>
    /// 显示/隐藏处理
    /// </summary>
    /// <param name="canvasGroup"></param>
    /// <param name="targetAlpha">目标透明度</param>
    /// <param name="smoothing">显示/隐藏速度</param>
    public static void HandleShowHide(CanvasGroup canvasGroup, float targetAlpha, float smoothing = 10) {
        if (canvasGroup.alpha != targetAlpha) {
            canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha, targetAlpha, smoothing * Time.deltaTime);
            if (Mathf.Abs(canvasGroup.alpha - targetAlpha) < 0.01f) {
                canvasGroup.alpha = targetAlpha;
            }
        }
    }

    public static void FollowPosition(GameObject gameObject, Vector2 screenPoint, Camera cam, Vector2 offset) {
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(gameObject.transform as RectTransform, Input.mousePosition, null, out position);
        //monoBehaviour.transform.localPosition = position + offset;
    }
    
    /// <summary>
    /// 跟随鼠标移动
    /// </summary>
    /// <param name="canvas">画布</param>
    /// <param name="monoBehaviour"></param>
    /// <param name="offset">偏移量</param>
    public static void FollowMouse(Canvas canvas, MonoBehaviour monoBehaviour, Vector2 offset) {
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, null, out position);
        monoBehaviour.transform.localPosition = position + offset;
    }

    public static void FollowMouse(Canvas canvas, MonoBehaviour monoBehaviour) {
        FollowMouse(canvas, monoBehaviour, Vector2.zero);
    }

    public static IEnumerator LoadSceneAsync(string sceneName, Dictionary<string, AsyncOperation> asyncOperationDictionary) {
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
        asyncOperation.allowSceneActivation = false;
        asyncOperationDictionary.Add("AsyncOperation", asyncOperation);
        if (!"Fight".Equals(sceneName)) {
            GlobalData.Instance.SceneName = sceneName;
        }
        yield return asyncOperation;
    }

    public static void UpdateSceneSlider(Dictionary<string, AsyncOperation> asyncOperationDictionary, Slider slider) {
        AsyncOperation asyncOperation = null;
        string key = "AsyncOperation";
        if (asyncOperationDictionary.ContainsKey(key)) {
            asyncOperation = asyncOperationDictionary[key];
        }
        if (asyncOperation == null) {
            return;
        }

        SetValue(slider, asyncOperation.progress);
        
        if (asyncOperation.progress >= 0.9f) {
            SetValue(slider, 1);
            asyncOperation.allowSceneActivation = true;
        }
    }

    private static void SetValue(Slider slider, float value) {
        if (slider != null) {
            slider.value = value;
        }
    }

    /// <summary>
    /// 显示模型标题
    /// </summary>
    /// <param name="target">模型</param>
    /// <param name="title">标题</param>
    /// <param name="x">x方向偏移量</param>
    /// <param name="y">y方向偏移量</param>
    /// <param name="z">z方向偏移量</param>
    /// <returns></returns>
    public static ModelTitle ShowModelTitle(GameObject target, string title, float x = 0, float y = 1, float z = 0) {
        Transform transform = GameObject.Find("Canvas").transform;
        ModelTitle ModelTitlePrefab = Resources.Load<ModelTitle>("Prefabs/ModelTitle");
        ModelTitle modelTitle = GameObject.Instantiate<ModelTitle>(ModelTitlePrefab, transform, false);
        modelTitle.transform.SetSiblingIndex(0); // 第一个渲染，避免遮住其他模型
        modelTitle.Target = target;
        modelTitle.Text.text = title;
        modelTitle.OffsetX = x;
        modelTitle.OffsetY = y;
        modelTitle.OffsetZ = z;
        return modelTitle;
    }
}
